home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / fortran / f2c_src.zip / F2C / PREAD.C < prev    next >
C/C++ Source or Header  |  1991-06-10  |  17KB  |  882 lines

  1. /****************************************************************
  2. Copyright 1990 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25.  
  26.  static char Ptok[128], Pct[Table_size];
  27.  static char *Pfname;
  28.  static long Plineno;
  29.  static int Pbad;
  30.  static int *tfirst, *tlast, *tnext, tmax;
  31.  
  32. #define P_space    1
  33. #define P_anum    2
  34. #define P_delim    3
  35. #define P_slash    4
  36.  
  37. #define TGULP    100
  38.  
  39.  static void
  40. trealloc()
  41. {
  42.     int k = tmax;
  43.     tfirst = (int *)realloc((char *)tfirst,
  44.         (tmax += TGULP)*sizeof(int));
  45.     if (!tfirst) {
  46.         fprintf(stderr,
  47.         "Pfile: realloc failure!\n");
  48.         exit(2);
  49.         }
  50.     tlast = tfirst + tmax;
  51.     tnext = tfirst + k;
  52.     }
  53.  
  54.  static void
  55. badchar(c)
  56.  int c;
  57. {
  58.     fprintf(stderr,
  59.         "unexpected character 0x%.2x = '%c' on line %ld of %s\n",
  60.         c, c, Plineno, Pfname);
  61.     exit(2);
  62.     }
  63.  
  64.  static void
  65. bad_type()
  66. {
  67.     fprintf(stderr,
  68.         "unexpected type \"%s\" on line %ld of %s\n",
  69.         Ptok, Plineno, Pfname);
  70.     exit(2);
  71.     }
  72.  
  73.  static void
  74. badflag(tname, option)
  75.  char *tname, *option;
  76. {
  77.     fprintf(stderr, "%s type from `f2c -%s` on line %ld of %s\n",
  78.         tname, option, Plineno, Pfname);
  79.     Pbad++;
  80.     }
  81.  
  82.  static void
  83. detected(msg)
  84.  char *msg;
  85. {
  86.     fprintf(stderr,
  87.     "%sdetected on line %ld of %s\n", msg, Plineno, Pfname);
  88.     Pbad++;
  89.     }
  90.  
  91.  static void
  92. checklogical(k)
  93.  int k;
  94. {
  95.     static int lastmsg = 0;
  96.     static int seen[2] = {0,0};
  97.  
  98.     seen[k] = 1;
  99.     if (seen[1-k]) {
  100.         if (lastmsg < 3) {
  101.             lastmsg = 3;
  102.             detected(
  103.     "Illegal combination of LOGICAL types -- mixing -I4 with -I2 or -i2\n\t");
  104.             }
  105.         return;
  106.         }
  107.     if (k) {
  108.         if (tylogical == TYLONG || lastmsg >= 2)
  109.             return;
  110.         if (!lastmsg) {
  111.             lastmsg = 2;
  112.             badflag("LOGICAL", "I4");
  113.             }
  114.         }
  115.     else {
  116.         if (tylogical == TYSHORT || lastmsg & 1)
  117.             return;
  118.         if (!lastmsg) {
  119.             lastmsg = 1;
  120.             badflag("LOGICAL", "i2` or `f2c -I2");
  121.             }
  122.         }
  123.     }
  124.  
  125.  static void
  126. checkreal(k)
  127. {
  128.     static int warned = 0;
  129.     static int seen[2] = {0,0};
  130.  
  131.     seen[k] = 1;
  132.     if (seen[1-k]) {
  133.         if (warned < 2)
  134.             detected("Illegal mixture of -R and -!R ");
  135.         warned = 2;
  136.         return;
  137.         }
  138.     if (k == forcedouble || warned)
  139.         return;
  140.     warned = 1;
  141.     badflag("REAL return", k ? "!R" : "R");
  142.     }
  143.  
  144.  static void
  145. Pnotboth(e)
  146.  Extsym *e;
  147. {
  148.     if (e->curno)
  149.         return;
  150.     Pbad++;
  151.     e->curno = 1;
  152.     fprintf(stderr,
  153.     "%s cannot be both a procedure and a common block (line %ld of %s)\n",
  154.         e->fextname, Plineno, Pfname);
  155.     }
  156.  
  157.  static int
  158. numread(pf, n)
  159.  register FILE *pf;
  160.  int *n;
  161. {
  162.     register int c, k;
  163.  
  164.     if ((c = getc(pf)) < '0' || c > '9')
  165.         return c;
  166.     k = c - '0';
  167.     for(;;) {
  168.         if ((c = getc(pf)) == ' ') {
  169.             *n = k;
  170.             return c;
  171.             }
  172.         if (c < '0' || c > '9')
  173.             break;
  174.         k = 10*k + c - '0';
  175.         }
  176.     return c;
  177.     }
  178.  
  179.  static void argverify(), Pbadret();
  180.  
  181.  static int
  182. readref(pf, e, ftype)
  183.  register FILE *pf;
  184.  Extsym *e;
  185.  int ftype;
  186. {
  187.     register int c, *t;
  188.     int i, nargs, type;
  189.     Argtypes *at;
  190.     Atype *a, *ae;
  191.  
  192.     if (ftype > TYSUBR)
  193.         return 0;
  194.     if ((c = numread(pf, &nargs)) != ' ') {
  195.         if (c != ':')
  196.             return c == EOF;
  197.         /* just a typed external */
  198.         if (e->extstg == STGUNKNOWN) {
  199.             at = 0;
  200.             goto justsym;
  201.             }
  202.         if (e->extstg == STGEXT) {
  203.             if (e->extype != ftype)
  204.                 Pbadret(ftype, e);
  205.             }
  206.         else
  207.             Pnotboth(e);
  208.         return 0;
  209.         }
  210.  
  211.     tnext = tfirst;
  212.     for(i = 0; i < nargs; i++) {
  213.         if ((c = numread(pf, &type)) != ' '
  214.         || type >= 500
  215.         || type != TYFTNLEN + 100 && type % 100 > TYSUBR)
  216.             return c == EOF;
  217.         if (tnext >= tlast)
  218.             trealloc();
  219.         *tnext++ = type;
  220.         }
  221.  
  222.     if (e->extstg == STGUNKNOWN) {
  223.  save_at:
  224.         at = (Argtypes *)
  225.             gmem(sizeof(Argtypes) + (nargs-1)*sizeof(Atype), 1);
  226.         at->nargs = nargs;
  227.         at->changes = 0;
  228.         t = tfirst;
  229.         a = at->atypes;
  230.         for(ae = a + nargs; a < ae; a++) {
  231.             a->type = *t++;
  232.             a->cp = 0;
  233.             }
  234.  justsym:
  235.         e->extstg = STGEXT;
  236.         e->extype = ftype;
  237.         e->arginfo = at;
  238.         }
  239.     else if (e->extstg != STGEXT) {
  240.         Pnotboth(e);
  241.         }
  242.     else if (!e->arginfo) {
  243.         if (e->extype != ftype)
  244.             Pbadret(ftype, e);
  245.         else
  246.             goto save_at;
  247.         }
  248.     else
  249.         argverify(ftype, e);
  250.     return 0;
  251.     }
  252.  
  253.  static int
  254. comlen(pf)
  255.  register FILE *pf;
  256. {
  257.     register int c;
  258.     register char *s, *se;
  259.     char buf[128], cbuf[128];
  260.     int refread;
  261.     long L;
  262.     Extsym *e;
  263.  
  264.     if ((c = getc(pf)) == EOF)
  265.         return 1;
  266.     if (c == ' ') {
  267.         refread = 0;
  268.         s = "comlen ";
  269.         }
  270.     else if (c == ':') {
  271.         refread = 1;
  272.         s = "ref: ";
  273.         }
  274.     else {
  275.  ret0:
  276.         if (c == '*')
  277.             ungetc(c,pf);
  278.         return 0;
  279.         }
  280.     while(*s) {
  281.         if ((c = getc(pf)) == EOF)
  282.             return 1;
  283.         if (c != *s++)
  284.             goto ret0;
  285.         }
  286.     s = buf;
  287.     se = buf + sizeof(buf) - 1;
  288.     for(;;) {
  289.         if ((c = getc(pf)) == EOF)
  290.             return 1;
  291.         if (c == ' ')
  292.             break;
  293.         if (s >= se || Pct[c] != P_anum)
  294.             goto ret0;
  295.         *s++ = c;
  296.         }
  297.     *s-- = 0;
  298.     if (s <= buf || *s != '_')
  299.         return 0;
  300.     strcpy(cbuf,buf);
  301.     *s-- = 0;
  302.     if (*s == '_') {
  303.         *s-- = 0;
  304.         if (s <= buf)
  305.             return 0;
  306.         }
  307.     for(L = 0;;) {
  308.         if ((c = getc(pf)) == EOF)
  309.             return 1;
  310.         if (c == ' ')
  311.             break;
  312.         if (c < '0' && c > '9')
  313.             goto ret0;
  314.         L = 10*L + c - '0';
  315.         }
  316.     if (!L && !refread)
  317.         return 0;
  318.     e = mkext(buf, cbuf);
  319.     if (refread)
  320.         return readref(pf, e, (int)L);
  321.     if (e->extstg == STGUNKNOWN) {
  322.         e->extstg = STGCOMMON;
  323.         e->maxleng = L;
  324.         }
  325.     else if (e->extstg != STGCOMMON)
  326.         Pnotboth(e);
  327.     else if (e->maxleng != L) {
  328.         fprintf(stderr,
  329.     "incompatible lengths for common block %s (line %ld of %s)\n",
  330.                     buf, Plineno, Pfname);
  331.         if (e->maxleng < L)
  332.             e->maxleng = L;
  333.         }
  334.     return 0;
  335.     }
  336.  
  337.  static int
  338. Ptoken(pf, canend)
  339.  FILE *pf;
  340.  int canend;
  341. {
  342.     register int c;
  343.     register char *s, *se;
  344.  
  345.  top:
  346.     for(;;) {
  347.         c = getc(pf);
  348.         if (c == EOF) {
  349.             if (canend)
  350.                 return 0;
  351.             goto badeof;
  352.             }
  353.         if (Pct[c] != P_space)
  354.             break;
  355.         if (c == '\n')
  356.             Plineno++;
  357.         }
  358.     switch(Pct[c]) {
  359.         case P_anum:
  360.             if (c == '_')
  361.                 badchar(c);
  362.             s = Ptok;
  363.             se = s + sizeof(Ptok) - 1;
  364.             do {
  365.                 if (s < se)
  366.                     *s++ = c;
  367.                 if ((c = getc(pf)) == EOF) {
  368.  badeof:
  369.                     fprintf(stderr,
  370.                     "unexpected end of file in %s\n",
  371.                         Pfname);
  372.                     exit(2);
  373.                     }
  374.                 }
  375.                 while(Pct[c] == P_anum);
  376.             ungetc(c,pf);
  377.             *s = 0;
  378.             return P_anum;
  379.  
  380.         case P_delim:
  381.             return c;
  382.  
  383.         case P_slash:
  384.             if ((c = getc(pf)) != '*') {
  385.                 if (c == EOF)
  386.                     goto badeof;
  387.                 badchar('/');
  388.                 }
  389.             if (canend && comlen(pf))
  390.                 goto badeof;
  391.             for(;;) {
  392.                 while((c = getc(pf)) != '*') {
  393.                     if (c == EOF)
  394.                         goto badeof;
  395.                     if (c == '\n')
  396.                         Plineno++;
  397.                     }
  398.  slashseek:
  399.                 switch(getc(pf)) {
  400.                     case '/':
  401.                         goto top;
  402.                     case EOF:
  403.                         goto badeof;
  404.                     case '*':
  405.                         goto slashseek;
  406.                     }
  407.                 }
  408.         default:
  409.             badchar(c);
  410.         }
  411.     /* NOT REACHED */
  412.     return 0;
  413.     }
  414.  
  415.  static int
  416. Pftype()
  417. {
  418.     switch(Ptok[0]) {
  419.         case 'C':
  420.             if (!strcmp(Ptok+1, "_f"))
  421.                 return TYCOMPLEX;
  422.             break;
  423.         case 'E':
  424.             if (!strcmp(Ptok+1, "_f")) {
  425.                 /* TYREAL under forcedouble */
  426.                 checkreal(1);
  427.                 return TYREAL;
  428.                 }
  429.             break;
  430.         case 'H':
  431.             if (!strcmp(Ptok+1, "_f"))
  432.                 return TYCHA